﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for UTIL
/// </summary>
public class UTIL
{
    public const string PageError = "~/other/ErrorPage.aspx";
    public const string PageLogin = "~/other/login.aspx";
    public const string PageDefault = "~/other/default.aspx";
    public const string PageNotAllowed = "~/other/NotAllowed.aspx";

    public static string ToEnglishCharacter(string word)
    {
        char[] Wordin = (word.ToLower()).ToCharArray();
        string result = string.Empty;
        for (int i = 0; i < Wordin.Length; i++)
        {
            switch (Wordin[i])
            {
                case 'ç': Wordin[i] = 'c'; break;
                case 'ğ': Wordin[i] = 'g'; break;
                case 'ı': Wordin[i] = 'i'; break;
                case 'ö': Wordin[i] = 'o'; break;
                case 'ş': Wordin[i] = 's'; break;
                case 'ü': Wordin[i] = 'u'; break;
            }
            result += Wordin[i].ToString();
        }
        return result;
    }

    public static string ReplaceSpace(string p)
    {
        p = p.ToLower();
        p = p.Replace(" ", "_");
        p = p.Replace(Environment.NewLine, string.Empty);
        p = p.Replace(".", "_");
        p = p.Replace("/", "_");
        p = p.Replace(":", "_");
        p = p.Replace(";", "_");
        return p;
    }

    public static string FirstUpperAllLower(string text)
    {
        string newString = string.Empty;
        string[] textArr = text.Split(' ');
        if (textArr.Length > 1)
        {
            for (int i = 0; i < textArr.Length; i++)
            {
                string tempString = string.Empty;
                tempString = textArr[i].ToLower();
                tempString = char.ToUpper(tempString[0]) + tempString.Substring(1);
                if (newString == string.Empty)
                {
                    newString = tempString;
                }
                else
                {
                    newString = newString + " " + tempString;
                }
            }
        }
        else
        {
            text = text.ToLower();
            text = char.ToUpper(text[0]) + text.Substring(1);
            newString = text;
        }
        return newString;
    }

    public static string CutString(string stringToShorten, int newLength, bool dotted)
    {
        string newString = string.Empty;
        if (newLength > stringToShorten.Length)
        {
            newString = stringToShorten;
        }
        else
        {
            int cutOffPoint = stringToShorten.IndexOf(" ", newLength - 1);
            if (cutOffPoint <= 0)
            {
                cutOffPoint = stringToShorten.Length;
                newString = stringToShorten.Substring(0, cutOffPoint);
            }
            else
            {
                newString = stringToShorten.Substring(0, cutOffPoint);
                if (dotted) newString = newString + "...";
            }

        }
        return newString;
    }
       
    public static string SafeSqlLiteral(System.Object theValue, System.Object theLevel)
    {

        // Written by user CWA, CoolWebAwards.com Forums. 2 February 2010
        // http://forum.coolwebawards.com/threads/12-Preventing-SQL-injection-attacks-using-C-NET

        // intLevel represent how thorough the value will be checked for dangerous code
        // intLevel (1) - Do just the basic. This level will already counter most of the SQL injection attacks
        // intLevel (2) -   (non breaking space) will be added to most words used in SQL queries to prevent unauthorized access to the database. Safe to be printed back into HTML code. Don't use for usernames or passwords

        string strValue = (string)theValue;
        int intLevel = (int)theLevel;

        if (strValue != null)
        {
            if (intLevel > 0)
            {
                strValue = strValue.Replace("'", "''"); // Most important one! This line alone can prevent most injection attacks
                strValue = strValue.Replace("--", "");
                strValue = strValue.Replace("[", "[[]");
                strValue = strValue.Replace("%", "[%]");
            }
            if (intLevel > 1)
            {
                string[] myArray = new string[] { "xp_ ", "update ", "insert ", "select ", "drop ", "alter ", "create ", "rename ", "delete ", "replace " };
                int i = 0;
                int i2 = 0;
                int intLenghtLeft = 0;
                for (i = 0; i < myArray.Length; i++)
                {
                    string strWord = myArray[i];
                    Regex rx = new Regex(strWord, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    MatchCollection matches = rx.Matches(strValue);
                    i2 = 0;
                    foreach (Match match in matches)
                    {
                        GroupCollection groups = match.Groups;
                        intLenghtLeft = groups[0].Index + myArray[i].Length + i2;
                        strValue = strValue.Substring(0, intLenghtLeft - 1) + "&nbsp;" + strValue.Substring(strValue.Length - (strValue.Length - intLenghtLeft), strValue.Length - intLenghtLeft);
                        i2 += 5;
                    }
                }
            }
            return strValue;
        }
        else
        {
            return strValue;
        }
    }
	
}